<script name="CallbackBind" lang="ts" setup>
import {QQUserInfo,QQUserInfoRes} from '@/types/user'
import useStore from '@/store';
import { ref } from 'vue';
import Message from '@/components/message';
import { useRouter } from 'vue-router';
const {user}=useStore()
const router=useRouter()
//api:
//QC.api('get_use_info')
//QC.Login.signout()退出QQ
//QC.Login.check() 检测是否登录
// QC.Login.getMe(function(openId){
// console.log(openId);
// })

// 获取用户信息
const qqInfo=ref<QQUserInfo>({} as QQUserInfo)
const isLogin=QC.Login.check()
let  openId=''
// 判断是否登录
if(isLogin){
    // QC.api的每次调用都是一个Request对象。
    QC.api('get_user_info').success((res:QQUserInfoRes)=>{
        // console.log(res)
         qqInfo.value=res.data
    })
    // 获取openid
    QC.Login.getMe(function(id){
        openId=id
    })
}
// 发送验证码
const mobile=ref('')
const code=ref('')
let time=ref(0)
 const send=async ()=>{
   await user.sendQQBindMsg(mobile.value)
    time.value=60
   let timeId=-1
   timeId=window.setInterval(()=>{
      time.value--
      if(time.value===0){
        clearInterval(timeId)
      }
    },1000)
 }

// QQ绑定
const bind=async ()=>{
  await user.qqBindLogin(openId,mobile.value,code.value)
  Message.success('QQ绑定成功')
  router.push('/')
}


</script>
<template>
  <div class="xtx-form">
    <div class="user-info">
      <img
        :src="qqInfo.figureurl_qq"
        alt=""
      />
      <p>Hi，{{qqInfo.nickname}} 欢迎来小兔鲜，完成绑定后可以QQ账号一键登录哦~</p>
    </div>
    <div class="xtx-form-item">
      <div class="field">
        <i class="icon iconfont icon-phone"></i>
        <input class="input" v-model="mobile" type="text" placeholder="绑定的手机号" />
      </div>
      <div class="error"></div>
    </div>
    <div class="xtx-form-item">
      <div class="field">
        <i class="icon iconfont icon-code"></i>
        <input class="input" type="text" placeholder="短信验证码"  v-model="code" />
        <span class="code" @click="send">{{time===0?'发送验证码':`${time}s后发送`}}</span>
      </div>
      <div class="error"></div>
    </div>
    <a href="javascript:;" class="submit" @click="bind">立即绑定</a>
  </div>
</template>

<style scoped lang="less">
.user-info {
  width: 320px;
  height: 70px;
  margin: 0 auto;
  display: flex;
  background: #f2f2f2;
  align-items: center;
  padding: 0 10px;
  margin-bottom: 25px;
  img {
    background: #f2f2f2;
    width: 50px;
    height: 50px;
  }
  p {
    padding-left: 10px;
  }
}
.code {
  position: absolute;
  right: 0;
  top: 0;
  line-height: 50px;
  width: 80px;
  color: #999;
  &:hover {
    cursor: pointer;
  }
}
</style>
